﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace MoControls
{
    public abstract class XNARendererBase
        : IRenderer, IDisposable
    {
        #region Fields

        private XNAUIManager manager = null;
        protected SpriteBatch spriteBatch = null;
        private bool disposed = false;
        private BlendingModes blendingMode;        
        private DeviceStates deviceStates = null;
        private bool isBeginEnabled = false;

        #endregion

        #region Properties

        public IUIManager Manager
        {
            get
            {
                return this.manager;
            }
        }

        public BlendingModes BlendingMode
        {
            get
            {
                return this.blendingMode;
            }
            set
            {
                this.blendingMode = value;
            }
        }

        #endregion

        #region .ctor

        public XNARendererBase(XNAUIManager manager)
        {
            if (manager == null)
                throw new ArgumentNullException("[RenderBase] - Manager cannot be null");

            this.deviceStates = new DeviceStates();
            this.manager = manager;
            this.spriteBatch = new SpriteBatch(this.manager.GraphicsDevice);            
        }

        #endregion

        #region Methods

        #region Begin functions

        public virtual void Begin()
        {
            BlendState blendState = null;

            if (!this.isBeginEnabled)
            {
                switch (this.BlendingMode)
                {
                    case BlendingModes.None:
                        blendState = this.deviceStates.BlendState;
                        break;

                    case BlendingModes.AlphaBlend:
                        blendState = BlendState.AlphaBlend;
                        break;

                    case BlendingModes.Opaque:
                    default:
                        blendState = BlendState.Opaque;
                        break;
                }

                this.spriteBatch.Begin(SpriteSortMode.Immediate, blendState, this.deviceStates.SamplerState, this.deviceStates.DepthStencilState, this.deviceStates.RasterizerState);
            }
        }

        public virtual void End()
        {
            if (this.isBeginEnabled)
                this.spriteBatch.End();
        }

        #endregion

        #region Drawing Primitives

        public virtual void Draw(Texture2D texture, Rectangle destination, Color color)
        {         
        }

        public virtual void Draw(Texture2D texture, Rectangle destination, Rectangle source, Color color)
        {         
        }

        public virtual void Draw(Texture2D texture, int left, int top, Color color)
        {
        }

        public virtual void Draw(Texture2D texture, int left, int top, Rectangle source, Color color)
        {
        }

        #endregion

        #region IDisposable

        public void Dispose()
        {
            if (!this.disposed)
            {
                if (this.spriteBatch != null)
                {
                    this.spriteBatch.Dispose();
                    this.spriteBatch = null;
                }
                this.disposed = true;
            }
        }

        #endregion

        #endregion
    }
}
